package com.xnx3.wangmarket.plugin.innerAccount.controller.admin;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.xnx3.j2ee.service.SqlService;
import com.xnx3.j2ee.util.Page;
import com.xnx3.j2ee.util.Sql;
import com.xnx3.j2ee.vo.BaseVO;
import com.xnx3.wangmarket.admin.util.AliyunLog;
import com.xnx3.wangmarket.plugin.base.controller.BasePluginController;
import com.xnx3.wangmarket.plugin.innerAccount.entity.Department;

/**
 * 部门管理
 * @author 管雷鸣
 */
@Controller
@RequestMapping("/plugin/innerAccount/admin/department/")
public class DepartmentInnerAccountPluginController extends BasePluginController {

	@Resource
	private SqlService sqlService;

	/**
	 * 部门列表
	 */
	@RequestMapping("list${url.suffix}")
	public String list(HttpServletRequest request, Model model){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		AliyunLog.addActionLog(getSiteId(), "查看部门列表");
		
		Sql sql = new Sql(request);
		sql.setSearchColumn(new String[]{"name"});
	    //查询user数据表的记录总条数。 传入的user：数据表的名字为user
	    int count = sqlService.count("plugin_inneraccount_department", sql.getWhere());
	    //创建分页，并设定每页显示25条
	    Page page = new Page(count, 25, request);
	    //创建查询语句，只有SELECT、FROM，原生sql查询。其他的where、limit等会自动拼接
	    sql.setSelectFromAndPage("SELECT * FROM plugin_inneraccount_department", page);
	    sql.setDefaultOrderBy("id DESC");
	    //因只查询的一个表，所以可以将查询结果转化为实体类，用List接收。
	    List<Department> list = sqlService.findBySql(sql, Department.class);
	    
	    //将展示的列表数据记录传到页面以供显示
	    model.addAttribute("list", list);
	    //将分页信息传到页面以供显示底部分页
	    model.addAttribute("page", page);
		return "plugin/innerAccount/admin/department/list";
	}
	

	/**
	 * 添加、编辑
	 * @param id 若传入值，则是修改；若是0，则是新增
	 * @return
	 */
	@RequestMapping("edit${url.suffix}")
	public String edit(HttpServletRequest request, HttpServletResponse response, Model model,
			@RequestParam(value = "id", required = false , defaultValue="0") int id){
		if(!haveSuperAdminAuth()){
			return error(model, "无权使用");
		}
		Department department;
		if(id>0){
			//编辑
			department = sqlService.findById(Department.class, id);
			if(department == null){
				return error(model, "要修改的部门不存在");
			}
		}else{
			department = new Department();
		}
		
		model.addAttribute("department", department);
		return "plugin/innerAccount/admin/department/edit";
	}
	
	/**
	 * 部门保存
	 * @return
	 */
	@RequestMapping(value="save${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO save(Department departmentInput,
			HttpServletRequest request,Model model){
		if(!haveSuperAdminAuth()){
			return error("无权使用");
		}
		Department department;
		if(departmentInput.getId() == null || departmentInput.getId() == 0 ){
			//新增
			department = new Department();
		}else{
			//修改
			department = sqlService.findById(Department.class, departmentInput.getId());
			if(department == null){
				return error("要修改的部门不存在");
			}
		}
		
		department.setName(departmentInput.getName());
		sqlService.save(department);
		
		//刷新js缓存
		refreshGenerateCache();
		
		return success();
	}
	
	/**
	 * 部门删除
	 * @param id 要删除的部门的id
	 * @return
	 */
	@RequestMapping(value="delete${url.suffix}", method = RequestMethod.POST)
	@ResponseBody
	public BaseVO delete(HttpServletRequest request,Model model,
			@RequestParam(value = "id", required = false , defaultValue="0") int id){
		if(!haveSuperAdminAuth()){
			return error("无权使用");
		}
		if(id < 1){
			return error("您要删除哪个部门呢？");
		}
		
		Department department = sqlService.findById(Department.class, id);
		if(department == null){
			return error("要删除的部门不存在");
		}
		
		sqlService.delete(department);
		
		//刷新js缓存
		refreshGenerateCache();
		
		return success();
	}
	
	/**
	 * 刷新js的缓存，重新生成 departmentList.js
	 */
	private void refreshGenerateCache(){
		List<Department> list = sqlService.findAll(Department.class);
		new com.xnx3.wangmarket.plugin.innerAccount.generateCache.Department().departmentList(list);
	}
	
}